package redis

import (
	"context"
	"fmt"
	"web_app/settings"

	"github.com/go-redis/redis/v8"
)

var rdb *redis.Client
var ctx = context.Background()

func InitClient(ctg *settings.RedisConfig) (err error) {
	rdb = redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%d",
			ctg.Host,
			ctg.Port,
		),
		Password: ctg.Password,
		DB:       ctg.DB,
		PoolSize: ctg.PoolSize,
	})

	_, err = rdb.Ping(ctx).Result()
	if err != nil {
		return err
	}
	return nil
}

func Close() {
	_ = rdb.Close()
}
